package com.future.message.Utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import ch.ethz.ssh2.*;


public class downUtil {
    public final static String UPLOAD_PATH_PREFIX="/static/upload/";

    public static void main(String[] args) {
        //port:22是连接linux传输,3389是连接windos传输
        Connection connection = new Connection("120.27.194.228",22);
        //客户端要存储的下载文件的路径可以自己随便定义
        File newFile = new File("/usr/src/scientific/Static/user/menuFather/320/a31b01d5-a838-4a2b-90a6-44e81041954b.jpg" );
        String pcPath="C:\\indexDownLoadTemp\\";
        try(FileOutputStream fos = new FileOutputStream(newFile);) {
            System.out.println(123);
            connection.connect();
            System.out.println(123456);
            boolean isAuthenticated = connection.authenticateWithPassword("root","Lishoujia3301");
            if(isAuthenticated){
                SCPClient scpClient = connection.createSCPClient();
                //从目标文件服务器下载文件
                SCPInputStream sis = scpClient.get("/usr/src/scientific/Static/user/menuFather/30/a31b01d5-a838-4a2b-90a6-44e81041954b.jpg");
                //先将远程服务器的文件下载到用户的电脑某个磁盘内
                File f = new File("/usr/src");
                if(!f.exists()){
                    f.mkdirs();//不存在就创建文件夹
                }
                byte[] b = new byte[4096];
                int i;
                while ((i = sis.read(b)) != -1){
                    fos.write(b,0, i);
                }
                fos.flush();
                fos.close();
                sis.close();
                connection.close();
                System.out.println("download ok");
            }else{
                System.out.println("连接建立失败");
            }
        } catch (IOException e) {
            System.out.println(("下载文件异常="+e.toString()));
        }
    }

}

